package cs2340.flyingkiwi.todoapp;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * This class simulates making a new category.
 * 
 * @author Group Flying Kiwi
 * @version 15 Mar 2012
 * 
 * It simply has a textfield for title, confirm and cancel buttons.
 * Pressing back button also cancels this activity.
 */

public class EditCategory extends Activity{
	private EditText mTitleText;
	private String accountId;
    private Long mRowId;
    private ToDoAppDbAdapter mDbHelper;
    
    /**
	 * onCreate method
	 * Displays the edit_category view and gets or creates a Category id. Saves
	 * the data in the text fields if Create Category button is clicked.
	 * Does not save data if Cancel is clicked.
	 * @param savedInstanceState
	 */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbHelper = new ToDoAppDbAdapter(this);
        mDbHelper.open();
        
        setContentView(R.layout.edit_category);
        setTitle(R.string.menu_edit);

        mTitleText = (EditText) findViewById(R.id.category_title);
        
        Button confirmButton = (Button) findViewById(R.id.category_edit_button);
        Button cancelButton = (Button) findViewById(R.id.cancel);
        
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            
            if (extras != null) {
            	Log.d("EditCategory onCreate", "create extras.size = " + extras.size());
            	if(extras.size() == 0) {
            		accountId = null;
            		mRowId = null;
            	
            	} 
	            else {
	                accountId = extras.getString(ToDoAppDbAdapter.KEY_CATEGORY_ACCOUNTID);
	                Log.d("EditCategory onCreate", "edit extras.size = " + extras.size());
	                if(extras.size() == 2)
	                	mRowId = extras.getLong(ToDoAppDbAdapter.KEY_CATEGORY_ROWID);
	                else
	                	mRowId = null;
	            }
            }
        } 
        else {
            accountId = (String) savedInstanceState.getSerializable(ToDoAppDbAdapter.KEY_CATEGORY_ACCOUNTID);
            mRowId = (Long) savedInstanceState.getSerializable(ToDoAppDbAdapter.KEY_CATEGORY_ROWID);
        }
        
        populateFields();
        
        //Set up buttons.
        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                if(mTitleText.toString().length() != 0)
                {
                	saveState(); //Save the result when the button is clicked
                	setResult(RESULT_OK);
                	finish();
                }
                else
                {	
                   	setResult(RESULT_CANCELED);
                	finish();
                }
            }

        });
        
        cancelButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            	Toast.makeText(EditCategory.this, "Canceled",
        				Toast.LENGTH_LONG).show();
               	setResult(RESULT_CANCELED);
               	finish();
            }

        });
    }
    
    /**
     * This method fetches the database and gets previous title when editing category.
     */
    
    private void populateFields(){
    	if(mRowId != null){
    		Cursor category = mDbHelper.fetchCategory(mRowId);
    		startManagingCursor(category);
    		mTitleText.setText(category.getString(
    				category.getColumnIndexOrThrow(ToDoAppDbAdapter.KEY_CATEGORY_TITLE)));
    	}
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
    
    /**
     * This method saves the user input.
     */
	private void saveState(){
	    	
	    String title = mTitleText.getText().toString();
	    	
	    //When there is no row id, create new category.
	    if(mRowId == null){
	    	Toast.makeText(EditCategory.this, title + " updated!", Toast.LENGTH_LONG).show();
	    	long id = mDbHelper.createCategory(title, accountId);
	    	if(id>0){
	    		mRowId = id;
	    	}
	    }
	    //otherwise, update the category.
	    else{
	    	Toast.makeText(EditCategory.this, title + " updated!", Toast.LENGTH_LONG).show();
	    	mDbHelper.updateCategory(mRowId, title);
	    }
	    
	}    
	public long getRowId(){
		return mRowId;
	}
}
